package com.bicon.botu.mall.coupon.api;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.bicon.botu.core.base.common.controller.BaseController;
import com.bicon.botu.core.base.common.entity.BaseResult;
import com.bicon.botu.mall.coupon.common.CouponStatus;
import com.bicon.botu.mall.coupon.dto.MyCouponsDto;
import com.bicon.botu.mall.coupon.dto.MyCouponsPramDto;
import com.bicon.botu.mall.coupon.entity.Coupons;
import com.bicon.botu.mall.coupon.service.ICouponsService;
import com.bicon.botu.mall.coupon.service.IMyCouponsService;
import com.bicon.botu.mall.goods.dto.ProductRepParamDto;
import com.bicon.botu.mall.goods.rpc.service.ProductRepertoryRpc;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * 我的优惠券 前端对接接口
 * @author dsp
 * @since 2017-07-19
 */
@RestController
@RequestMapping(value = "/V3.0.0/api/MyCoupons")
public class MyCouponsApiV3 extends BaseController{

    @Autowired
    IMyCouponsService iMyCouponsService;

    @Autowired
    ICouponsService iCouponsService;

    @Autowired
    ProductRepertoryRpc thirdPartRpc;

    /**
     * 根据优惠券状态 查询我的优惠券列表
     * status 状态可选字段
     * userId 用户ID
     * @param status
     * @return
     */
    @RequestMapping(value = "getMyCoupListByUserIdOrStatus",method = RequestMethod.POST)
    public BaseResult getMyCouponsListByStatus(@RequestParam(defaultValue = "") String status,String userId){
        List<MyCouponsDto> myCouponsDtos=new ArrayList<>();
        if(StringUtils.isNotBlank(userId)){
            myCouponsDtos=iMyCouponsService.getMyCouponsListByStatus(status,userId);
        }else {
            return  sendResult400();
        }
        return sendResult200(myCouponsDtos);
    }

    /**
     * 使用优惠券后，计算订单总金额
     * @param jsonParms
     * @return
     */
    @RequestMapping(value = "calculateTotalMoney",method = RequestMethod.POST)
    public BaseResult calculateTotalMoney(String jsonParms){
        if (StringUtils.isNotBlank(jsonParms)){
            //返回值数据封装对象
            JSONObject resultJson=new JSONObject();
            JSONArray resultArray=new JSONArray();
            BigDecimal totalMoney=new BigDecimal(0);    //订单总额
            //JSONArray 数据解析
            List<MyCouponsPramDto> pramDtoList=JSONArray.parseArray(jsonParms,MyCouponsPramDto.class);
            //判断数据接收集合
            if(pramDtoList != null && pramDtoList.size()>0){
                for (MyCouponsPramDto myCouponsPramDto:pramDtoList){
                    //必传参数检验 校验规则：商户ID不能为空、优惠券ID不能为空、商品列表数量不能为0
                    if(StringUtils.isNotBlank(myCouponsPramDto.getStoreId())
                            && myCouponsPramDto.getProductRepList() !=null && myCouponsPramDto.getProductRepList().size()>0 ){
                        //根据传参ID 获得优惠券详情   业务修改 此处暂是查询两次
                        BigDecimal discount=new BigDecimal(0);
                        if (StringUtils.isNotBlank(myCouponsPramDto.getCouponsId())){
                            Coupons coupons=iCouponsService.selectById(iMyCouponsService.selectById(myCouponsPramDto.getCouponsId()).getCouponsId());
                            discount=coupons.getDiscountAmount();
                        }
                        List<ProductRepParamDto> productRepParamDtos=JSONArray.parseArray(thirdPartRpc.getProductRepInfo(myCouponsPramDto.getProductRepList()), ProductRepParamDto.class);
                        if( productRepParamDtos.size()>0){
                            //存储商家，所有商品总金额
                            BigDecimal product_big=new BigDecimal(0);
                            for (ProductRepParamDto repParamDto : productRepParamDtos){
                                //根据某一商品的金额和数量 计算金额总和
                                BigDecimal b1 = new BigDecimal(repParamDto.getAmount());
                                product_big=product_big.add(b1.multiply(repParamDto.getPrice()));
                            }
                            //构造返回JSON格式对象
                            JSONObject resultStoreJson=new JSONObject();
                            resultStoreJson.put("storeId",myCouponsPramDto.getStoreId());
                            resultStoreJson.put("discount",discount.doubleValue());
                            BigDecimal subtractValue=product_big.subtract(discount);
                            if(product_big.compareTo(discount)>0){
                                resultStoreJson.put("storeTotalMoney",subtractValue);
                                totalMoney=totalMoney.add(subtractValue);
                            }else{
                                resultStoreJson.put("storeTotalMoney",product_big);
                                totalMoney=totalMoney.add(product_big);
                            }
                            resultArray.add(resultStoreJson);
                        }else{
                            return sendResult(CouponStatus.RESPONSE_CODE_603,CouponStatus.RESPONSE_MESSAGE_603);
                        }
                    }else{
                        return sendResult400();
                    }
                }
            }else{
                return sendResult400();
            }
            //封装返回对象
            resultJson.put("storeList",resultArray);
            resultJson.put("totalMoney",totalMoney.doubleValue());
            return sendResult200(resultJson);
        }else{
            return sendResult400();
        }
    }
}
